#include "peterbus.h"


#define PB_TYPE_RPM  0x01
#define PB_TYPE_PWM  0x02

unsigned char data[32];

unsigned long delta;

int rpm1 = 0;
int rpm2 = 0;
int rpm3 = 0;
int rpm4 = 0x7d;


void pb_send_engine_rpm()
{
  rpm1 = analogRead(A0);
  data[0]=rpm1 & 0x00FF;
  data[1]=(rpm1 & 0xFF00)>>8;
  data[2]=rpm2 & 0x00FF;
  data[3]=(rpm2 & 0xFF00)>>8;
  data[4]=rpm3 & 0x00FF;
  data[5]=(rpm3 & 0xFF00)>>8;
  data[6]=rpm4 & 0x00FF;
  data[7]=(rpm4 & 0xFF00)>>8;
  
  pb_write(PB_TYPE_RPM,8,data);
}


void setup()
{
  Serial.begin(2400);
  pb_init();
  
  delta = millis();
}


void loop()
{  
  
  unsigned char id;
  unsigned char len;
 
  unsigned char light;
  
  light = analogRead(A0);
  
  if(light>160)
  {
     rpm3=rpm3+1; 
  }
   
   if((millis()-delta)>=1000)
   {
     rpm1=rpm3;
     rpm3=0;
     rpm2=light;
     delta = millis();
     pb_send_engine_rpm();
   }
   
  /*
  if(pb_read(&id,&len,data))
  {
    if(id==PB_TYPE_PWM)
    {
       pb_send_engine_rpm(); 
    }
  }
  */
  
  //delay(100);
}
